package cn.hellochaos.generator.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
@Slf4j
public class FileUtil {
    public static ArrayList<String> getFiles(String path) {
        ArrayList<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
//              System.out.println("文     件：" + tempList[i]);
                files.add(tempList[i].toString());
            }
            if (tempList[i].isDirectory()) {
//              System.out.println("文件夹：" + tempList[i]);
            }
        }
        return files;
    }

    /**
     *
     * @param path
     * @return
     */
    public static String getOneFileNameFromFolder(String path) {
        log.info("path is {}",path);
        ArrayList<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        /**
         * 空文件夹，没有文件的情况
         */
        if(tempList.length==0) {
            return null;
        }

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
//              System.out.println("文     件：" + tempList[i]);
                files.add(tempList[i].toString());
            }
        }
        //必须关闭流，file不是流，不用关，只是路径的抽象
        return files.get(0);
    }


}
